package com.example.myretrofit2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myretrofit2.common.ImageLoaderManager;
import com.example.myretrofit2.common.MyApplication;
import com.nostra13.universalimageloader.core.ImageLoader;

public class AdActivity extends AppCompatActivity {
private ImageView imageView;
Button button;
TextView textView;
Thread thread;
Handler handler;
Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad);
        imageView = findViewById(R.id.ad_image);
        button = findViewById(R.id.btn_image);
        textView = findViewById(R.id.tv_image);
        ImageLoader.getInstance().displayImage(MyApplication.imageUrl+"1.png",imageView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toMain();
                handler.removeCallbacks(runnable);
            }
        });
        time();
    }
    public void time(){
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                if (MyApplication.AD_TIME >= 0 ){
                    setText();
                    MyApplication.AD_TIME--;
                    handler.postDelayed(runnable,1000);
                }
            }
        };
        runnable.run();
    }
    public void setText(){
        if(MyApplication.AD_TIME > 0)
            textView.setText(MyApplication.AD_TIME + "s");
        else
            toMain();//跳转到主界面Intent

    }
    public void toMain(){
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }
}